package controller;

import java.util.ArrayList;

import model.*;
import model.container.*;
import persistency.reader.*;
import settings.PathSettings;

public class ContainerController {

	private static ContainerController containerController;
	
	private CourseContainer courseContainer;
	private RegistrationContainer registrationContainer;
	private TeacherContainer teacherContainer;
	private StudentContainer studentContainer;
	
	/* Make sure the class cannot be instantiated */
	private ContainerController(){
		
	}
	
	
	
	
	/** Returns the existing instance of the controller or constructs a new object */
	public static synchronized ContainerController getInstance(){
		if (containerController == null) {
			containerController = new ContainerController();
		}
		return containerController;
	}
	
	public void generateTestData(){
		//init containers
		courseContainer = CourseContainer.getInstance();
		registrationContainer = RegistrationContainer.getInstance();
		studentContainer = StudentContainer.getInstance();
		teacherContainer = TeacherContainer.getInstance();
		
		courseContainer.generateData();
		registrationContainer.generateData();
		studentContainer.generateData();
		teacherContainer.generateData();
		
		courseContainer.finishInitialisation();
		registrationContainer.finishInitialisation();
		studentContainer.finishInitialisation();
		teacherContainer.finishInitialisation();
		
		courseContainer.finishCourseOfferedCourseInitiatialisation();
	}
	
	
	/** Reads all data from the text files */
	public void readAllData() {
		readCourses();
		readRequiredCourses();
		readTeachers();
		readTeacherPreferences();
		readStudents();
		readOfferedCourses();
		readCourseHistory();
		readRegistrations();
		readRegistrationOfferedCourses();
	}
	
	/* Read course objects from text file */
	private void readCourses() {
		try
		{
			// Get instance of course container
			CourseContainer courseContainer = CourseContainer.getInstance();
			
			// Create a new course reader
			CourseReader courseReader = new CourseReader(PathSettings.COURSEFILE);
			
			// Open the text file
			courseReader.open();

			// Read the courses into the container
			courseContainer.setCourses(courseReader.read());
			
			// Close the text file
			courseReader.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	/* Read course objects from text file */
	private void readRequiredCourses() {
		try
		{
			// Get instance of course container
			// CourseContainer courseContainer = CourseContainer.getInstance();
			
			// Create a new course reader
			RequiredCourseReader requiredCourseReader = new RequiredCourseReader(PathSettings.REQUIREDCOURSEFILE);
			
			// Open the text file
			requiredCourseReader.open();

			// Read the courses into the container
			requiredCourseReader.read();
			// courseContainer.setCourses);

			// Close the text file
			requiredCourseReader.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	/* Read course objects from text file */
	private void readCourseHistory() {
		try
		{
			// Get instance of course container
			ArrayList<CourseHistory> elements = new ArrayList<CourseHistory>();
			
			// Create a new course reader
			CourseHistoryReader courseHistoryReader = new CourseHistoryReader(PathSettings.COURSEHISTORYFILE);
			
			// Open the text file
			courseHistoryReader.open();

			// Read the courses into the container
			elements = courseHistoryReader.read();
			
			// Close the text file
			courseHistoryReader.close();
			
			
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	/* Read teacher preferences objects from text file */
	private void readTeacherPreferences() {
		try
		{
			// Get instance of course container
			ArrayList<Teacher> teachers = new ArrayList<Teacher>();
			
			// Create a new course reader
			TeacherPreferenceReader teacherPreferenceReader = new TeacherPreferenceReader(PathSettings.TEACHERCOURSEPREFERENCEFILE);
			
			// Open the text file
			teacherPreferenceReader.open();

			// Read the courses into the container
			teachers = teacherPreferenceReader.read();
			
			// Close the text file
			teacherPreferenceReader.close();
			
			
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}

	/* Read teachers objects from text file */
	private void readStudents() {
		try
		{
			// Get instance of teacher container
			StudentContainer studentContainer = StudentContainer.getInstance();
			
			// Create a new teacher reader
			StudentReader studentReader = new StudentReader();
			studentReader.setFilePath(PathSettings.STUDENTFILE);
			studentReader.setFreeStudentFilePath(PathSettings.FREESTUDENTFILE);
			studentReader.setRetrainingStudentFilePath(PathSettings.RETRAININGSTUDENTFILE);
			
			// Open the text file
			studentReader.open();

			// Read the teacher into the container
			studentContainer.setStudents(studentReader.read());
			
			// Close the text file
			studentReader.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	/* Read teachers objects from text file */
	private void readTeachers() {
		try
		{
			// Get instance of teacher container
			TeacherContainer teacherContainer = TeacherContainer.getInstance();
			
			// Create a new teacher reader
			TeacherReader teacherReader = new TeacherReader(PathSettings.TEACHERFILE);
			
			// Open the text file
			teacherReader.open();

			// Read the teacher into the container
			teacherContainer.setTeachers(teacherReader.read());
			
			// Close the text file
			teacherReader.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	/* Read offered course objects from text file */
	private void readOfferedCourses() {
		try
		{
			// Get instance of offered course container
			OfferedCourseContainer offeredCourseContainer = OfferedCourseContainer.getInstance();
			
			// Create a new course reader
			OfferedCourseReader offeredCourseReader = new OfferedCourseReader(PathSettings.OFFEREDCOURSEFILE);
			
			// Open the text file
			offeredCourseReader.open();

			// Read the courses into the container
			offeredCourseContainer.setOfferedCourses(offeredCourseReader.read());
			
			// Close the text file
			offeredCourseReader.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	/* Read offered course objects from text file */
	private void readRegistrations() {
		try
		{
			// Get instance of registration container
			RegistrationContainer registrationContainer = RegistrationContainer.getInstance();
			
			// Create a new registration reader
			RegistrationReader registrationReader = new RegistrationReader(PathSettings.REGISTRATIONFILE);
			
			// Open the text file
			registrationReader.open();

			// Read the courses into the container
			registrationContainer.setRegistrations(registrationReader.read());
			
			// Close the text file
			registrationReader.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}

	/* Read teacher preferences objects from text file */
	private void readRegistrationOfferedCourses() {
		try
		{
			// Get instance of course container
			//ArrayList<Teacher> teachers = new ArrayList<Teacher>();
			
			// Create a new course reader
			RegistrationOfferedCourseReader rocr = new RegistrationOfferedCourseReader(PathSettings.OFFEREDCOURSEREGISTRATIONFILE);
			
			// Open the text file
			rocr.open();
	
			// Read the courses into the container
			rocr.read();
			
			// Close the text file
			rocr.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
